package com.xgry.bpm.api.base.reviewer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.xgry.bpm.api.base.reviewer.service.ReviewerService;
import com.xgry.bpm.api.business.departmentalmanagement.entity.DepartmentalManagement;
import com.xgry.bpm.api.business.departmentalmanagement.mapper.DepartmentalManagementMapper;
import com.xgry.bpm.api.business.ersonnelfiles.entity.PersonnelFiles;
import com.xgry.bpm.api.business.ersonnelfiles.mapper.PersonnelFilesMapper;
import com.xgry.bpm.api.business.personnelfileschild.entity.PersonnelFilesChild;
import com.xgry.bpm.api.business.personnelfileschild.mapper.PersonnelFilesChildMapper;
import com.xgry.bpm.api.business.postfiles.entity.PostFiles;
import com.xgry.bpm.api.business.postfiles.mapper.PostFilesMapper;
import com.xgry.bpm.api.base.userInfo.entity.UserInfo;
import com.xgry.bpm.api.base.userInfo.mapper.UserInfoMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Slf4j
@Service
@Transactional
public class ReviewerServiceImpl implements ReviewerService {

    @Autowired
    private UserInfoMapper userInfoDao;

    @Autowired
    private PostFilesMapper postFilesMapper;

    @Autowired
    private PersonnelFilesMapper personnelFilesMapper;

    @Autowired
    private PersonnelFilesChildMapper personnelFilesChildMapper;

    @Autowired
    private DepartmentalManagementMapper departmentalManagementMapper;

    @Override
    public String findDepartmentHead(String userName) {
        List<UserInfo> byUserName = userInfoDao.selectList(new QueryWrapper<UserInfo>(){{
            this.eq("userName",userName);
        }});
        PersonnelFiles personnelFiles = personnelFilesMapper.selectList(new QueryWrapper<PersonnelFiles>() {{
            this.eq("PersonnelFiles_id", byUserName.get(0).getPersonnelId());
        }}).get(0);
        //再通过部门id去查询部门负责人
        DepartmentalManagement departmentalManagement = departmentalManagementMapper.selectList(new QueryWrapper<DepartmentalManagement>() {{
            this.eq("DepartmentalManagement_id", personnelFiles.getDepartmentalmanagementId());
        }}).get(0);

        UserInfo userInfo = userInfoDao.selectList(new QueryWrapper<UserInfo>() {{
            this.eq("personnelId", departmentalManagement.getDepartmentalmanagementDepartmentshead());
        }}).get(0);

        return userInfo.getUserId().toString();
    }

    @Override
    public String findDepartmentalLeadership(String userName) {
        List<UserInfo> byUserName = userInfoDao.selectList(new QueryWrapper<UserInfo>(){{
            this.eq("userName",userName);
        }});
        PersonnelFiles personnelFiles = personnelFilesMapper.selectList(new QueryWrapper<PersonnelFiles>() {{
            this.eq("PersonnelFiles_id", byUserName.get(0).getPersonnelId());
        }}).get(0);
        //再通过部门id去查询部门负责人
        DepartmentalManagement departmentalManagement = departmentalManagementMapper.selectList(new QueryWrapper<DepartmentalManagement>() {{
            this.eq("DepartmentalManagement_id", personnelFiles.getDepartmentalmanagementId());
        }}).get(0);

        UserInfo userInfo = userInfoDao.selectList(new QueryWrapper<UserInfo>() {{
            this.eq("personnelId", departmentalManagement.getDepartmentalmanagementDepartmentalleadership());
        }}).get(0);

        return userInfo.getUserId().toString();
    }

    @Override
    public String findPerviousApprover(String approverId) {
        UserInfo byUserName = userInfoDao.selectById(approverId);
        PersonnelFiles personnelFiles = personnelFilesMapper.selectList(new QueryWrapper<PersonnelFiles>() {{
            this.eq("PersonnelFiles_id", byUserName.getPersonnelId());
        }}).get(0);
        //再通过部门id去查询部门负责人
        DepartmentalManagement departmentalManagement = departmentalManagementMapper.selectList(new QueryWrapper<DepartmentalManagement>() {{
            this.eq("DepartmentalManagement_id", personnelFiles.getDepartmentalmanagementId());
        }}).get(0);

        UserInfo userInfo = userInfoDao.selectList(new QueryWrapper<UserInfo>() {{
            this.eq("personnelId", departmentalManagement.getDepartmentalmanagementDepartmentshead());
        }}).get(0);

        return userInfo.getUserId().toString();
    }

    @Override
    public String findSuperiorPosition(String userName) {
        List<UserInfo> userInfos = userInfoDao.selectList(new QueryWrapper<UserInfo>(){{
            this.eq("userName",userName);
        }});

        List<PersonnelFilesChild> personnelFilesChildList = personnelFilesChildMapper.selectList(new QueryWrapper<PersonnelFilesChild>() {{
            this.eq("PersonnelFiles_id", userInfos.get(0).getPersonnelId());
        }});

        String parentLevel = "未查询到信息";

        if (CollectionUtils.isEmpty(personnelFilesChildList)) {
            return parentLevel;
        }

        PersonnelFilesChild personnelFilesChild = personnelFilesChildList.get(0);
        PostFiles postFiles = postFilesMapper.selectList(new QueryWrapper<PostFiles>() {{
            this.eq("PostFiles_id", personnelFilesChild.getPersonnelfileschildPost());
        }}).get(0);
        parentLevel = postFiles.getParentLevel();
        UserInfo userInfo = userInfoDao.selectList(new QueryWrapper<UserInfo>() {{
            this.eq("personnelId", postFiles.getParentLevel());
        }}).get(0);
        parentLevel = userInfo.getUserId().toString();

        return parentLevel;
    }

    @Override
    public String findPerviousApproverSP(String approverId) {
        UserInfo userInfo = userInfoDao.selectById(approverId);
        List<PersonnelFilesChild> personnelFilesChildList = personnelFilesChildMapper.selectList(new QueryWrapper<PersonnelFilesChild>() {{
            this.eq("PersonnelFiles_id", userInfo.getPersonnelId());
        }});
        String parentLevel = "未查询到信息";

        if (CollectionUtils.isEmpty(personnelFilesChildList)) {
            return parentLevel;
        }

        PersonnelFilesChild personnelFilesChild = personnelFilesChildList.get(0);
        PostFiles postFiles = postFilesMapper.selectList(new QueryWrapper<PostFiles>() {{
            this.eq("PostFiles_id", personnelFilesChild.getPersonnelfileschildPost());
        }}).get(0);
        UserInfo queryUserInfo = userInfoDao.selectList(new QueryWrapper<UserInfo>() {{
            this.eq("personnelId", postFiles.getParentLevel());
        }}).get(0);
        parentLevel = queryUserInfo.getUserId().toString();

        return parentLevel;
    }

    @Override
    public String findDirector(String username) {
        List<UserInfo> userInfos = userInfoDao.selectList(new QueryWrapper<UserInfo>(){{
            this.eq("userName",username);
        }});

        List<PersonnelFiles> personnelFiles = personnelFilesMapper.selectList(new QueryWrapper<PersonnelFiles>() {{
            this.eq("PersonnelFiles_id", userInfos.get(0).getPersonnelId());
        }});

        List<DepartmentalManagement> departmentalManagements = departmentalManagementMapper.selectList(new QueryWrapper<DepartmentalManagement>() {{
            this.eq("DepartmentalManagement_id", personnelFiles.get(0).getDepartmentalmanagementId());
        }});

        UserInfo queryUserInfo = userInfoDao.selectList(new QueryWrapper<UserInfo>() {{
            this.eq("personnelId", departmentalManagements.get(0).getDepartmentalmanagementCentredirector());
        }}).get(0);

        return queryUserInfo.getUserId().toString();
    }
}
